# Contributor: Runar Tenfjord < runar dot tenfjord at gmail dot com >
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=cython
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-cython" "${MINGW_PACKAGE_PREFIX}-cython2")
pkgver=0.23.5
pkgrel=1
pkgdesc="C-Extensions for Python (mingw-w64)"
arch=('any')
url="http://www.cython.org"
license=('APACHE')
makedepends=(
            "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
            "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
            "${MINGW_PACKAGE_PREFIX}-gcc"
            )
source=("http://cython.org/release/Cython-${pkgver}.tar.gz")
sha256sums=('0ae5a5451a190e03ee36922c4189ca2c88d1df40a89b4f224bc842d388a0d1b6')

prepare() {
  cp -a ${_realname}-${pkgver} ${_realname}-py2-${pkgver}-${CARCH}
  mv ${_realname}-${pkgver} ${_realname}-${pkgver}-${CARCH}
}

package_cython() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools")
  cd ${_realname}-${pkgver}-${CARCH}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  DISTUTILS_DEBUG=1 \
    ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
                                --root="${pkgdir}" --optimize=1

  # Our setuptools {gui,cli}{,-32,-64}.exe have special-case handling for /usr/bin/env such that
  # they look in PATH.
  sed -i 's|#!.*|#!/usr/bin/env python3|' ${pkgdir}/${MINGW_PREFIX}/bin/cy{gdb,thon}-script.py
}

package_cython2() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools")
  cd ${_realname}-py2-${pkgver}-${CARCH}
  DISTUTILS_DEBUG=1 \
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
                                --root="${pkgdir}" --optimize=1

  # Our setuptools {gui,cli}{,-32,-64}.exe have special-case handling for /usr/bin/env such that
  # they look in PATH.
  sed -i 's|#!.*|#!/usr/bin/env python2|' ${pkgdir}/${MINGW_PREFIX}/bin/cy{gdb,thon}-script.py

  mv ${pkgdir}${MINGW_PREFIX}/bin/cygdb{,2}.exe
  mv ${pkgdir}${MINGW_PREFIX}/bin/cython{,2}.exe
  mv ${pkgdir}${MINGW_PREFIX}/bin/cythonize{,2}.exe
  if [ -f ${pkgdir}${MINGW_PREFIX}/bin/cygdb.exe.manifest ]; then
    mv ${pkgdir}${MINGW_PREFIX}/bin/cygdb{,2}.exe.manifest
    mv ${pkgdir}${MINGW_PREFIX}/bin/cython{,2}.exe.manifest
    mv ${pkgdir}${MINGW_PREFIX}/bin/cythonize{,2}.exe.manifest
  fi

  mv ${pkgdir}${MINGW_PREFIX}/bin/cygdb{,2}-script.py
  mv ${pkgdir}${MINGW_PREFIX}/bin/cython{,2}-script.py
  mv ${pkgdir}${MINGW_PREFIX}/bin/cythonize{,2}-script.py
}

package_mingw-w64-i686-cython() {
  package_cython
}

package_mingw-w64-i686-cython2() {
  package_cython2
}

package_mingw-w64-x86_64-cython() {
  package_cython
}

package_mingw-w64-x86_64-cython2() {
  package_cython2
}
